/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.core.ws;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import pe.core.db.DBConnect;
import pe.core.ws.beans.ActualizaResult;
import pe.core.ws.beans.AnalistaResult;
import pe.core.ws.beans.ConsecionarioResult;
import pe.core.ws.beans.CronogramaResult;
import pe.core.ws.beans.EnviaAreaCentralResult;
import pe.core.ws.beans.ModeloResult;
import pe.core.ws.beans.UbigeoResult;

/**
 *
 * @author romulogalindo
 */
@WebService(serviceName = "Santander")
public class Santander {

    @Resource
    WebServiceContext wsContext;

    @WebMethod(operationName = "ws_acceso_validar_sesion")
    public long ws_acceso_validar_sesion(
            @WebParam(name = "no_usuari") String no_usuari,
            @WebParam(name = "pw_usuari") String pw_usuari
    ) {
        //Parametros
        String remoteip = "";
        String navegador = "";

        //conseguir los valores
        MessageContext mc = wsContext.getMessageContext();
        HttpServletRequest req = (HttpServletRequest) mc.get(MessageContext.SERVLET_REQUEST);
        remoteip = req.getRemoteAddr();

        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(pw_usuari.getBytes());
            byte byteData[] = digest.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < byteData.length; i++) {
                sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
            }
            pw_usuari = sb.toString();
        } catch (NoSuchAlgorithmException ep) {
        }

        //crear el query
        String query = " select id_sesion from sesion.ppregsesini('" + no_usuari + "', '" + pw_usuari + "', '" + remoteip + "', '', '" + navegador + "',0);";
        DBConnect con = new DBConnect();
        long id_sesion = con.getSesion(query);
        System.out.println("[Standander ws][WS:ws_acceso_validar_sesion] Ejecutado correctamente.");
        return id_sesion;
    }

    @WebMethod(operationName = "ws_acceso_evalucion_analista")
    public AnalistaResult ws_acceso_evalucion_analista(
            @WebParam(name = "id_sesion") long id_sesion,
            @WebParam(name = "co_conces") int co_conces,
            @WebParam(name = "no_apepat_tit") String no_apepat_tit,
            @WebParam(name = "no_apemat_tit") String no_apemat_tit,
            @WebParam(name = "no_nombre_tit") String no_nombre_tit,
            @WebParam(name = "ti_docide_tit") String ti_docide_tit,
            @WebParam(name = "nu_docide_tit") String nu_docide_tit,
            @WebParam(name = "fe_nacimi_tit") String fe_nacimi_tit,//en formato dd-MM-yyyy
            @WebParam(name = "ti_genero_tit") String ti_genero_tit,
            @WebParam(name = "ti_deplab_tit") String ti_deplab_tit,
            @WebParam(name = "ti_forlab_tit") String ti_forlab_tit,
            @WebParam(name = "im_ingnet_tit") double im_ingnet_tit,
            @WebParam(name = "ti_antlab_tit") int ti_antlab_tit,
            @WebParam(name = "ti_vivien") String ti_vivien,
            @WebParam(name = "ti_estciv") String ti_estciv,
            @WebParam(name = "no_apepat_con") String no_apepat_con,
            @WebParam(name = "no_apemat_con") String no_apemat_con,
            @WebParam(name = "no_nombre_con") String no_nombre_con,
            @WebParam(name = "ti_docide_con") String ti_docide_con,
            @WebParam(name = "nu_docide_con") String nu_docide_con,
            @WebParam(name = "fe_nacimi_con") String fe_nacimi_con,//en formato dd-MM-yyyy
            @WebParam(name = "ti_genero_con") String ti_genero_con,
            @WebParam(name = "ti_deplab_con") String ti_deplab_con,
            @WebParam(name = "ti_forlab_con") String ti_forlab_con,
            @WebParam(name = "im_ingnet_con") double im_ingnet_con,
            @WebParam(name = "ti_antlab_con") int ti_antlab_con
    ) {
        //procesar data..
        String query = "select * from f_an_sanftp(" + id_sesion + "::integer, " + co_conces + "::smallint,'" + no_apepat_tit + "'::varchar , '" + no_apemat_tit + "'::varchar , '" + no_nombre_tit + "'::varchar , '" + ti_docide_tit + "'::varchar , '" + nu_docide_tit + "'::varchar , '" + fe_nacimi_tit + "'::date, '" + ti_genero_tit + "'::varchar ,  '" + ti_deplab_tit + "'::varchar , '" + ti_forlab_tit + "'::varchar , " + im_ingnet_tit + "::numeric, " + ti_antlab_tit + "::smallint,  '" + ti_vivien + "'::varchar, '" + ti_estciv + "'::varchar , '" + no_apepat_con + "'::varchar, '" + no_apemat_con + "'::varchar , '" + no_nombre_con + "'::varchar ,'" + ti_docide_con + "'::varchar, '" + nu_docide_con + "'::varchar, '" + fe_nacimi_con + "'::date, '" + ti_genero_con + "'::varchar,  '" + ti_deplab_con + "'::varchar , '" + ti_forlab_con + "'::varchar , " + im_ingnet_con + "::numeric , " + ti_antlab_con + "::smallint)";
        DBConnect con = new DBConnect();
        AnalistaResult analista = con.getEvaluacionAnalista(query);
        if (analista != null) {
            System.out.println("[Standander ws][WS:ws_acceso_evalucion_analista] Ejecutado correctamente.");
            return analista;
        } else {
            System.out.println("[Standander ws][WS:ws_acceso_evalucion_analista] Ejecutado correctamente.");
            return new AnalistaResult(0, "ERROR");
        }
    }

    @WebMethod(operationName = "ws_acceso_parametro_simulador_consecionario")
    public List<ConsecionarioResult> ws_acceso_parametro_simulador_consecionario(
            @WebParam(name = "co_sesion") long co_sesion
    ) {
        List<ConsecionarioResult> consecionarios = new ArrayList<>();

        //crear query
        String query = "select * from public.f_an_sanconautftp(" + co_sesion + "::integer);";

        DBConnect con = new DBConnect();
        consecionarios = con.getConsecionarios(query);
        System.out.println("[Standander ws][WS:ws_acceso_parametro_simulador_consecionario] Ejecutado correctamente.");
        return consecionarios;
    }

    @WebMethod(operationName = "ws_acceso_parametro_simulador_modelo")
    public List<ModeloResult> ws_acceso_parametro_simulador_modelo(
            @WebParam(name = "co_expedi") long co_expedi
    ) {
        List<ModeloResult> modelos = new ArrayList<>();

        //crear query
        String query = "select * from public.f_an_sanmodautftp(" + co_expedi + "::integer);";

        DBConnect con = new DBConnect();
        modelos = con.getModelos(query);
        System.out.println("[Standander ws][WS:ws_acceso_parametro_simulador_modelo] Ejecutado correctamente.");
        return modelos;
    }

    @WebMethod(operationName = "ws_acceso_simulador_cronograma")
    public List<CronogramaResult> ws_acceso_simulador_cronograma(
            @WebParam(name = "id_sesion") long id_sesion,
            @WebParam(name = "co_expedi") long co_expedi,
            @WebParam(name = "co_modelo") int co_modelo,
            @WebParam(name = "im_preveh") double im_preveh,
            @WebParam(name = "im_cuoini") double im_cuoini,
            @WebParam(name = "nu_cuotas") int nu_cuotas,
            @WebParam(name = "fe_priven") String fe_priven,
            @WebParam(name = "co_asegur") int co_asegur
    ) {
        //select * from public.f_an_sanmodautftp(25580118::integer); 
        List<CronogramaResult> cronograma = null;
        //crear query
        String query = "select * from f_si_simsanftp(" + id_sesion + "::bigint, " + co_expedi + "::bigint, " + co_modelo + "::integer, " + im_preveh + "::numeric, " + im_cuoini + "::numeric, " + nu_cuotas + "::smallint, '" + fe_priven + "'::date, " + co_asegur + "::smallint);";

        DBConnect con = new DBConnect();
        cronograma = con.getCronograma(query);
        System.out.println("[Standander ws][WS:ws_acceso_simulador_cronograma] Ejecutado correctamente.");
        return cronograma;
    }

    @WebMethod(operationName = "ws_acceso_actualiza_datos")
    public ActualizaResult ws_acceso_actualiza_datos(
            @WebParam(name = "id_sesion") long id_sesion,
            @WebParam(name = "co_expedi") long co_expedi,
            @WebParam(name = "p_ti_solici") String p_ti_solici,
            @WebParam(name = "ti_domici") String ti_domici,
            @WebParam(name = "ti_camino") int ti_camino,
            @WebParam(name = "no_camino") String no_camino,
            @WebParam(name = "nu_camino") String nu_camino,
            @WebParam(name = "nu_manzan") String nu_manzan,
            @WebParam(name = "nu_lotiza") String nu_lotiza,
            @WebParam(name = "ti_interi") int ti_interi,
            @WebParam(name = "nu_interi") String nu_interi,
            @WebParam(name = "ti_zongeo") String ti_zongeo,
            @WebParam(name = "no_zongeo") String no_zongeo,
            @WebParam(name = "no_refere") String no_refere,
            @WebParam(name = "nu_bloque") String nu_bloque,
            @WebParam(name = "nu_kilmet") String nu_kilmet,
            @WebParam(name = "no_barrio") String no_barrio,
            @WebParam(name = "nu_etapas") String nu_etapas,
            @WebParam(name = "nu_grupos") String nu_grupos,
            @WebParam(name = "nu_sector") String nu_sector,
            @WebParam(name = "no_parcel") String no_parcel,
            @WebParam(name = "nu_telefo") String nu_telefo,
            @WebParam(name = "nu_telper") String nu_telper,
            @WebParam(name = "co_ugibeo") String co_ugibeo
    //            @WebParam(name = "co_envacc") int co_envacc //Cambiado 03-09-2014
    ) {
        //crear query
        ActualizaResult actualiza = null;
        String query = "select * from f_si_dirsanftp(" + id_sesion + "::bigint, " + co_expedi + "::bigint, '" + p_ti_solici + "'::varchar, '" + ti_domici + "'::varchar, " + ti_camino + "::smallint, '" + no_camino + "'::varchar, '" + nu_camino + "'::varchar,'" + nu_manzan + "'::varchar, '" + nu_lotiza + "'::varchar, " + ti_interi + "::smallint, '" + nu_interi + "'::varchar, " + ti_zongeo + "::smallint, '" + no_zongeo + "'::varchar, '" + no_refere + "'::varchar,'" + nu_bloque + "'::varchar, '" + nu_kilmet + "'::varchar, '" + no_barrio + "'::varchar, '" + nu_etapas + "'::varchar, '" + nu_grupos + "'::varchar, '" + nu_sector + "'::varchar, '" + no_parcel + "'::varchar,'" + nu_telefo + "'::varchar, '" + nu_telper + "'::varchar, " + co_ugibeo + "::varchar)";
        DBConnect con = new DBConnect();
        actualiza = con.getActualizar(query);
        System.out.println("[Standander ws][WS:ws_acceso_actualiza_datos] Ejecutado correctamente.");
        return actualiza;
    }

    @WebMethod(operationName = "ws_acceso_envia_area_central")
    public EnviaAreaCentralResult ws_acceso_envia_area_central(
            @WebParam(name = "id_sesion") long id_sesion,
            @WebParam(name = "co_expedi") long co_expedi,
            @WebParam(name = "co_envacc") int co_envacc
    ) {
        EnviaAreaCentralResult enviaAreaCentral = null;
        String query = "select f_an_eacsanftp(" + id_sesion + "::bigint, " + co_expedi + "::bigint, " + co_envacc + "::smallint );";
        DBConnect con = new DBConnect();
        enviaAreaCentral = con.getEnviaAreaCentral(query);
        System.out.println("[Standander ws][WS:ws_acceso_envia_area_central] Ejecutado correctamente.");
        return enviaAreaCentral;
    }

    @WebMethod(operationName = "ws_acceso_ubigeos_lima")
    public List<UbigeoResult> ws_acceso_ubigeos_lima(
            @WebParam(name = "co_expedi") long co_expedi
    ) {
        List<UbigeoResult> ubigeo = null;
        String query = "select * from f_an_distsanftp(" + co_expedi + "::bigint);";
        DBConnect con = new DBConnect();
        ubigeo = con.getUbigeosLima(query);
        System.out.println("[Standander ws][WS:ws_acceso_ubigeos_lima] Ejecutado correctamente.");
        return ubigeo;
    }

}
